package view;

import java.awt.BorderLayout;
import java.io.File;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.ListSelectionModel;
import javax.swing.table.JTableHeader;

import controller.DiskContentPanelListener;
import controller.DiskContentPanelMouseListener;
import controller.TableHeaderSorter;
import controller.menubar.commands.FileSystemWatcher;
import model.FileTableModel;
import model.GlobalParams;
import model.TableRenderer;


public class DiskContentPanel extends JPanel {

	private static final long serialVersionUID = 1L;
	
	public DiskContentPanel(MainFrame f,BrowsePanel bf , int con) {		
	
		frame = f;
		browsePanel = bf;
		conntent = con;
		
		setLayout(new BorderLayout());
		root = new File(System.getProperty("user.home"));	
		
		//if (conntent == 1)
		scroll = new JScrollPane(CreateTable());
			
		add(scroll,BorderLayout.CENTER);	
		
	}	

	public void setNonActive() {
		//if (conntent == 1) {
			ListSelectionModel model = table.getSelectionModel();		
			model.removeSelectionInterval(table.getSelectedColumn(), table.getSelectedRow());	
	//	}
		//else {
		//	tree.removeSelectionInterval(0, 0);
		//}
	}
	
	public void setActive() {
		//if (conntent == 1) {
			ListSelectionModel model = table.getSelectionModel();		
			if (table.getSelectedColumn() != -1 && table.getSelectedRow()!= -1)
				model.addSelectionInterval(table.getSelectedColumn(), table.getSelectedRow());
			else 
				model.addSelectionInterval(0, 0);
		
	}		
	
	public JTable CreateTable(){
		
		table = new JTable();
		table.setDefaultRenderer(JLabel.class, new TableRenderer());
		
		modelTable = new FileTableModel(root);
		modelTable.Clear();
		modelTable.Create(root);
		modelTable.UpdateNames();		
		table.setModel(modelTable);		
		//table.setAutoCreateRowSorter(true);
		JTableHeader tableHeader = table.getTableHeader();
		TableHeaderSorter sorter = new TableHeaderSorter(frame);
		tableHeader.addMouseListener(sorter);
		
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setShowGrid(false);	
		table.setShowHorizontalLines(false);
		table.setShowVerticalLines(false);	
		table.getTableHeader().setReorderingAllowed(false);
		table.setFillsViewportHeight(true);		
		
		table.addMouseListener(new DiskContentPanelMouseListener(this,frame,browsePanel));
		table.addKeyListener(new DiskContentPanelListener(this,frame));
		
		table.getColumnModel().getColumn(0).setPreferredWidth(200);
		table.getColumnModel().getColumn(0).setMaxWidth(200);			
		table.getColumnModel().getColumn(1).setPreferredWidth(100);
		table.getColumnModel().getColumn(1).setMaxWidth(100);			
		table.getColumnModel().getColumn(2).setPreferredWidth(150);
		table.getColumnModel().getColumn(2).setMaxWidth(300);
		
		return table;
	}
	
	public void startScaning(File f) {		
		newSW = new FileSystemWatcher(frame,f);		
		newSW.execute();
	}
	

	synchronized public void CreateNewView(File file) {
		//if (conntent == 1) {				
			if (file != null) 
				root = file;	
			
			if (root.isDirectory()) {						
				((FileTableModel) table.getModel()).Clear();
				((FileTableModel) table.getModel()).Create(root);	
				((FileTableModel) table.getModel()).fireTableDataChanged();
				browsePanel.getRelativePath().setText(root.getAbsolutePath());
				browsePanel.setStatusBar(root);
			}			
		//}		
	}
	
	public File getSelectedFile() {		
		return modelTable.getSelectedRow(table.getSelectedRow());			
	}
	
	public String getSelectedFileName() {		
			JLabel l = modelTable.getSelectedRowName(table.getSelectedRow());				
			return l.getName();		
	}
	
	public void setActualFile(File file) {
		root = file;
	}
	
	public File getActualFile() {
		return root;
	}
	
	public JTable getTable() {
		return this.table;
	}
	
	public FileTableModel getTableModel() {		
		return (FileTableModel) table.getModel();
	}	
	
	
	public void updateNames(){
		//if (conntent == 1) {
			table.getTableHeader().getColumnModel().getColumn(0).setHeaderValue(GlobalParams.getInstance().stringCollection.getTable_file_name()); 
			table.getTableHeader().getColumnModel().getColumn(1).setHeaderValue(GlobalParams.getInstance().stringCollection.getTable_ext()); 
			table.getTableHeader().getColumnModel().getColumn(2).setHeaderValue(GlobalParams.getInstance().stringCollection.getTable_date()); 
			table.getTableHeader().getColumnModel().getColumn(3).setHeaderValue(GlobalParams.getInstance().stringCollection.getTable_size()); 
			table.getTableHeader().getColumnModel().getColumn(4).setHeaderValue(GlobalParams.getInstance().stringCollection.getTable_attr()); 
			table.repaint();		
		//}
	}	
	
	private File root;
	private JTree tree;	
	private JTable table;	
	private int conntent;
	private MainFrame frame;
	private JScrollPane scroll;	
	private BrowsePanel browsePanel;
	private FileTableModel modelTable;
	static public FileSystemWatcher newSW;
	
	
}
